package cn.pxs.controller;

import cn.pxs.entity.BookType;
import cn.pxs.service.BookTypeService;
import cn.pxs.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/api/adm/type")
public class BookTypeController {
    @Autowired
    private BookTypeService bookService;

    @RequestMapping("/findAll")
    @ResponseBody
    public Map findAll(){
        List<BookType> books = bookService.findAll();
        int n = 200;
        if(books == null){
            n = 400;
            return RespJson.pack(n,"失败",books);
        }
        return RespJson.pack(n,"成功",books);
    }

    @RequestMapping("/findAll2")
    @ResponseBody
    public Map findAll2(){
        List<BookType> books = bookService.findAll2();
        int n = 200;
        if(books == null){
            n = 400;
            return RespJson.pack(n,"失败",books);
        }
        return RespJson.pack(n,"成功",books);
    }

    @RequestMapping("/findById/{id}")
    @ResponseBody
    public Map findById(@PathVariable("id")Integer id){
        int n = 200;
        BookType book = bookService.findById(id);
        if(book == null){
            n = 400;
            return RespJson.pack(n,"失败",book);
        }
        return RespJson.pack(n,"成功",book);
    }


    @PostMapping("/add")
    @ResponseBody
    public Map updateOrAdd(@RequestBody BookType bookType){
        List<BookType> types = bookService.updateOrAdd(bookType);
        if(types == null || types.size()==0){
            return RespJson.pack(400,"操作失败",null);
        }
        return RespJson.pack(200,"操作成功",types);
    }

    @PostMapping("/add2")
    @ResponseBody
    public Map updateOrAdd2(@RequestBody BookType bookType){
        List<BookType> types = bookService.updateOrAdd2(bookType);
        if(types == null || types.size()==0){
            return RespJson.pack(400,"操作失败",null);
        }
        return RespJson.pack(200,"操作成功",types);
    }

}
